package com.briup.jz.service.impl;

import com.briup.jz.bean.AccountApply;
import com.briup.jz.bean.AccountApplyExample;
import com.briup.jz.bean.extend.AccountApplyExtend;
import com.briup.jz.dao.AccountApplyMapper;
import com.briup.jz.dao.extend.AccountApplyExtendMapper;
import com.briup.jz.service.IAccountApplayService;
import com.briup.jz.utils.CustomerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class AccountApplyImpl implements IAccountApplayService{
    @Autowired
    private AccountApplyMapper accountApplyMapper;

    @Autowired
    private AccountApplyExtendMapper accountApplyExtendMapper;

    @Override
    public void saveOrUpdate(AccountApply accountApply) {
        if(accountApply.getId()==null){
            accountApplyMapper.insert(accountApply);
        }else {
            accountApplyMapper.updateByPrimaryKey(accountApply);
        }

    }

    public List<AccountApply> query(Long id) {
        AccountApplyExample example=new AccountApplyExample();
        if(id!=null){
//            如果id不为空则按id查询 否则则查询所有
            example.createCriteria().andIdEqualTo(id);
        }

        return accountApplyMapper.selectByExample(example);
    }

    @Override
    public void deleteById(Long id) throws  CustomerException{
AccountApply accountApply=accountApplyMapper.selectByPrimaryKey(id);
if (accountApply==null){
    throw new CustomerException("你删除的申请信息不存在");
}
accountApplyMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void checkNoPass(Long id, String reason) throws CustomerException {
//        根据id找到该条评论
        AccountApply accountApply=accountApplyMapper.selectByPrimaryKey(id);
        if (accountApply==null){
//            如果没有这条评论则抛出异常
            throw  new CustomerException("没有这条申请");
        }
        accountApply.setStatus("不通过");
        accountApply.setReason(reason);
        saveOrUpdate(accountApply);
    }

    @Override
    public void checkPass(Long id) throws CustomerException {
        //        根据id找到该条评论
        AccountApply accountApply=accountApplyMapper.selectByPrimaryKey(id);
        if (accountApply==null){
            //            如果没有这条评论则抛出异常
            throw  new CustomerException("没有这条申请");
        }
        accountApply.setStatus("通过");
        saveOrUpdate(accountApply);
    }

    @Override
    public List<AccountApplyExtend> querywithuser(Long id) {
        List<AccountApplyExtend> accountApplyExtends=new ArrayList<>();
        if (id==null){
            accountApplyExtends= accountApplyExtendMapper.selectAll();
        }else {
            AccountApplyExtend accountApplyExtend = accountApplyExtendMapper.select(id);
            accountApplyExtends.add(accountApplyExtend);
        }
        return accountApplyExtends;


    }


}
